package edu.controller.impl;


import edu.domain.Student;
import edu.uitls.AgeUtils;

public class StudentController extends BasePersonController<Student> {
    @Override
    public void addPerson(String msg) {
        System.out.println("请输入添加的"+ msg+"学号:");
        String id = sc.nextLine();
        int index = personService.getIndexById(id);
        if (index == -1) {
            Student stu = getStudent(msg, id);
            if (stu == null) return;
            personService.addPerson(stu);

        }else {
            System.out.println("当前学号已存在,无法执行添加操作");
        }
    }

    public Student getStudent(String msg, String id) {
        System.out.println("请输入要添加的"+msg+"姓名");
        String name = sc.nextLine();
        String birthday , age;
        System.out.println("请输入要添加的"+msg+"生日");
        birthday = sc.nextLine();
        age = AgeUtils.getAge(birthday);
        if (age.equals("-1")){
            System.out.println("日期格式有误,请重试");
            return getStudent(msg,id);
        }
        Student stu = new Student(id,name,age,birthday);
        return stu;
    }


    @Override
    public void updatePerson(String msg) {
        System.out.println("请输入修改的学生学号:");
        String id = sc.nextLine();
        int index = personService.getIndexById(id);
        if (index != -1) {
            Student stu = getMsgPerson(id,msg);
            personService.updatePerson(index,stu);

        }else {
            System.out.println("当前学号不存在,无法执行修改操作");
        }
    }

    public Student getMsgPerson(String id ,String msg){
        String name , age , birthday;
        System.out.println("请输入要修改的学生姓名");
        name = sc.nextLine();
        System.out.println("请输入要添加的学生生日");
        birthday = sc.nextLine();
        age = AgeUtils.getAge(birthday);
        if (age.equals("-1")){
            System.out.println("日期格式有误,修改失败");
            return getMsgPerson(id,msg);
        }
        return new Student(id, name, age, birthday);
    }
}
